﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;

using Model.Hero;
using RemotingInterfaces;

namespace Model.Adventure
{
    [Serializable]
    public class Dungeon : MarshalByRefObject, IDungeon
    {        
        private String _name;
        private List<Encounter> _encounters;
        private Encounter _current_encounter;
        private bool _cleared;
        private ManualResetEvent _MRE;
        private Combat _combat;
        //Thread _thread;

        //assigned thread from world server
        public Dungeon(String name)
        {
            Console.WriteLine("{0} created", name);                        
            _cleared = false;
            _combat = new Combat();
            _MRE = _combat.MRE;
            _encounters = new List<Encounter>();
            _name = name;
        }

        public String Name
        {
            get { return _name; }
        }

        //called by the WaitCallback delegate
        public void EnterDungeon(object heroes)
        {
            EnterDungeon(heroes as List<Character>);
        }

        public void EnterDungeon(List<Character> heroes)
        {
            Console.WriteLine("Entering {0}", _name);
            //Console.WriteLine("#encounters = {0}", _encounters.Count);
            while (_encounters.Count > 0)
            {
                _current_encounter = _encounters.First();                
                Console.WriteLine("starting = {0}", _current_encounter.Name);                
                _current_encounter.StartEncounter(heroes as List<Character>, _combat);
                //remove encounter from list after loot, xp etc. is sorted
                _encounters.Remove(_current_encounter);
            }
            _cleared = true;
            Console.WriteLine("{0} cleared!", Name);
        }

        public void EnterDungeon()
        {
            _current_encounter = _encounters.First();             
        }

        public void AddEncounter(Encounter encounter)
        {
            _encounters.Add(encounter);
        }

        /*public IEncounter getActiveEncounter()
        {
            return _encounters.First();
        }//*/

        public bool Cleared
        {
            get { return _cleared; }
        }

        /*public Character PrimaryTarget
        {
            get { return _current_encounter == null ? null : _current_encounter.PrimaryTarget; }
            set
            {
                if (_current_encounter == null)
                    return;
                _current_encounter.PrimaryTarget = value;
                //notify, converter: target -> target name, null -> "none"
            }
        }//*/

        public ManualResetEvent MRE
        {
            get { return _MRE; }
        }

        //returns a randomly choosen monster from current encounter
        public ICharacter GetRandomMonster()
        {
            //Console.WriteLine("Dungeon::GetRandomMonster()");
            if (_current_encounter == null)
            {
                //Console.WriteLine("_current_encounter == null");
                return null;
            }            
            return _current_encounter.GetRandomMonster();
        }

        public override string ToString()
        {
            String str = "List of encounters for : " + Name + "\n";
            foreach (Encounter encounter in _encounters)
                str += encounter;
            return str;
        }
    }
}
